home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / WLIST.ARJ / LINKLIST.H < prev    next >
Text File  |  1992-05-16  |  4KB  |  97 lines

  1. #ifndef LinkListIncluded
  2. #define LinkListIncluded
  3.  
  4. // written by Paul Wheaton
  5. // 406-543-7543 (voice)   406-543-1144 (data)
  6. // CompuServe 72707,207
  7.  
  8. #include <stddef.h>
  9. #include <Misc.h>
  10.  
  11. struct LinkedListElement
  12.   {
  13.     friend class LinkedList;
  14.     LinkedListElement *PrevRec, *NextRec;
  15.     void* Rec;
  16.   };
  17.  
  18. class LinkedList
  19.   {
  20.       LinkedListElement *RootRec, *CurRec;
  21.       Long NumRecs;
  22.       Long CurRecNum; // 0...NumRecs-1
  23.     public:
  24.       LinkedList();
  25.         // an empty form
  26.       ~LinkedList();
  27.       void* Root();
  28.         // returns rec 0.  CurRec is set to RootRec.
  29.       void* Cur() {return CurRec->Rec;}
  30.       Long Size() {return NumRecs;}
  31.         // the number of things in list
  32.       Long Top() {return(NumRecs-1);}
  33.       Long Num() {return CurRecNum;}
  34.         // 0..Top():  Cur's index
  35.       void* Next();
  36.         // Cur is moved up and returned
  37.       void* Prev();
  38.         // Cur is moved back and returned
  39.       void* Last();
  40.         // like Root only it's the last element
  41.       Bool Insert(void* NewRec);
  42.         // will be inserted between Prev and Cur
  43.       Bool Add(void* NewRec);
  44.         // will be inserted between Cur and Next
  45.       Bool Append(void* NewRec);
  46.         // will be the last record in the list
  47.       Bool Push(void* NewRec){return Append(NewRec);}
  48.       void DelCur();
  49.         // link is destroyed.  Cur will be old Next.  Object is not deleted
  50.       void DelCurObj()  // same as DelCur cept object is destroyed too
  51.         // Note!  Only the object memory is freed.  No object destructors are called
  52.         {delete Cur(); DelCur();}
  53.       void* Pop();
  54.         // Returns pointer to last rec. Link is destroyed. Cur will be old Prev.
  55.       void* operator[](Long Index);
  56.  
  57.     //  to do:  add in stuff like Add(void* NewRec,...);
  58.  
  59.   };
  60.  
  61. #define CreateLinkedListClass(ClassName,ObjType)                        \
  62.                                                                         \
  63. class ClassName: public LinkedList                                      \
  64.   {                                                                     \
  65.     public:                                                             \
  66.       ObjType& Root(){return *(ObjType*)LinkedList::Root();}            \
  67.       ObjType& Last(){return *(ObjType*)LinkedList::Last();}            \
  68.       ObjType& Cur() {return *(ObjType*)LinkedList::Cur();}             \
  69.       ObjType& Next(){return *(ObjType*)LinkedList::Next();}            \
  70.       ObjType& Prev(){return *(ObjType*)LinkedList::Prev();}            \
  71.       ObjType& Pop() {return *(ObjType*)LinkedList::Pop();}             \
  72.       Bool Insert(ObjType& NewRec){return LinkedList::Insert(&NewRec);} \
  73.       Bool Add(ObjType& NewRec)   {return LinkedList::Add(&NewRec);}    \
  74.       Bool Append(ObjType& NewRec){return LinkedList::Append(&NewRec);} \
  75.       Bool Push(ObjType& NewRec)  {return LinkedList::Push(&NewRec);}   \
  76.       ObjType& operator[](Long Index)                                   \
  77.         {return *(ObjType*)(LinkedList::operator[](Index));}            \
  78.       void DelCurObj()                                                  \
  79.         {delete ((ObjType*)LinkedList::Cur()); LinkedList::DelCur();}   \
  80.       void DelAllObj(); /* {while (Size()) DelCurObj();} */             \
  81.   };
  82.  
  83. class Queue:public LinkedList
  84.   {
  85.     public:
  86.       Queue(){}
  87.       void* Pop();
  88.   };
  89.  
  90. class Stack:public LinkedList
  91.   {
  92.     public:
  93.       Stack(){}
  94.   };
  95.  
  96. #endif
  97.